<?php
require_once APPLICATION_PATH . "/lib/BaseModel.php";

class Friend extends Tamtay_Table_Abstract
{
    protected $_name = 'friend';
    protected $_primary = 'id';

    function getFriends($uid=0, $page=0, $limit=20, $sort="name ", $recount=true)
    {

        $uid=(int)$uid;
        if ($uid < 1) return ;
        
        $db=$this->getSlaveDB(); 
        $start=$page * $limit;
                
        $sql = "SELECT u.name, u.avatar, f.* 
                    FROM friend f 
                        INNER JOIN users u ON f.friendID = u.uid 
                    WHERE f.uid=$uid ORDER BY $sort LIMIT $start, $limit";
        $count=null;
        if ($recount)
        {
            $sql_count = "SELECT count(*) 
                        FROM friend f 
                            INNER JOIN users u ON f.friendID = u.uid 
                        WHERE f.uid=$uid";            
            $count=$this->execScalar($sql_count);
        }  
        $data=$this->execSelectQuery($sql);
        return array("data" => $data, "count" => $count);
    }
    
    // GET REQUEST FRIEND OF USER_ID
    function getReqFriends($uid=0, $page=1, $limit=20, $sort="timestamp DESC", $recount=true)
    {
        $db=$this->getSlaveDB(); 
        $start=$page * $limit;
        $uid=(int)$uid;
        if ($uid < 1) return ;
        $sql = "SELECT u.*
                    FROM users u 
                        INNER JOIN friend_temp ft ON u.uid = ft.uid 
                    WHERE ft.reqsID=$uid ORDER BY $sort LIMIT $start, $limit";

        $sql_count = "SELECT count(*) 
                    FROM users u 
                        INNER JOIN friend_temp ft ON u.uid = ft.uid 
                    WHERE ft.reqsID=$uid";
        $count=null;
        if ($recount)
        {
            $count=$this->execScalar($sql_count);
        }  
        $data=$this->execSelectQuery($sql);
        return array("data" => $data, "count" => $count);
    }
    
    function getSentFriends($uid=0, $page=1, $limit=20, $sort="timestamp DESC", $recount=true)
    {
        $db=$this->getSlaveDB(); 
        $start=$page * $limit;
        $uid=(int)$uid;
        if ($uid < 1) return ;
        $sql = "SELECT u.*, ft.id as request_id 
                    FROM users u 
                        INNER JOIN friend_temp ft ON u.uid = ft.reqsID 
                    WHERE ft.uid=$uid ORDER BY $sort LIMIT $start, $limit";

        $sql_count = "SELECT count(*) 
                    FROM users u 
                        INNER JOIN friend_temp ft ON u.uid = ft.reqsID 
                    WHERE ft.uid=$uid ";
        $count=null;
        if ($recount)
        {
            $count=$this->execScalar($sql_count);
        }  
        $data=$this->execSelectQuery($sql);
        return array("data" => $data, "count" => $count);
    }    
    
    // send a friend request
    function addFriend($uid, $friendID)
    {
        if ($uid  < 1 || $friendID < 1 || $uid===$friendID) return 0;
        $db = $this->getMasterDB();
        
        try 
        {
            $t = time();
            // add to friend_temp table 
            $sql = "INSERT INTO friend_temp VALUES ('', $uid, $friendID, $t)";

            return $db->query($sql);  
        }
        catch (Exception $e)
        {
            return 0;
        }        
        
        //return 1;
        
    }
    
    // remove friend out of friend_list
    function removeFriend($uid, $friendID)
    {
        if ($uid  < 1 || $friendID < 1 || $uid===$friendID) return 0;
        $db = $this->getMasterDB();
        
        try 
        {
            $t = time();
            $sql = "DELETE FROM friend WHERE (uid=$uid AND friendID=$friendID) OR (uid=$friendID AND friendID=$uid)";
            $db->query($sql);
        }
        catch (Exception $e)
        {
            return 2;
        }        
        
        return 1;
    }
    
    // accept to add a friend to friend_list
    function acceptFriend($uid, $friendID)
    {
        if ($uid  < 1 || $friendID < 1 || $uid===$friendID) return false;
        $db = $this->getMasterDB();
        
        try 
        {
            $t = time();
            // add to friend table
            $sql = "INSERT INTO friend VALUES ('', $uid, $friendID, 0, '', 0, $t)";

            $db->query($sql); 
            
            // add to friend table
            $sql = "INSERT INTO friend VALUES ('', $friendID, $uid, 0, '', 0, $t)";
            $db->query($sql); 
            
            // remove from friend_temp table
            $sql = "DELETE FROM friend_temp WHERE (uid=$uid AND reqsID=$friendID) OR (uid=$friendID AND reqsID=$uid)";
            $db->query($sql);
            
            return true; 
        }
        catch (Exception $e)
        {
            return false;
        }        
        
    }
    
    // ignore to friend
    function ignoreFriend($uid, $friendID)
    {
        if ($uid  < 1 || $friendID < 1 || $uid===$friendID) return 0;
        $db = $this->getMasterDB();
        
        try 
        {
            $t = time();
            $sql = "DELETE FROM friend_temp WHERE uid=$uid AND reqsID=$friendID";
            return $db->query($sql);
        }
        catch (Exception $e)
        {
            return 0;
        }        
    }
    
    // ignore to friend
    function checkExistFriend($uid, $friendID)
    {
        if ($uid  < 1 || $friendID < 1 || $uid===$friendID) return 0;
        $db = $this->getSlaveDB();
        
        try 
        {
            $t = time();
            $sql    = "SELECT uid FROM friend WHERE (uid=$uid AND friendID=$friendID)";
            $rs = $db->fetchOne($sql);
            return $rs;
        }
        catch (Exception $e)
        {
            return 0;
        }        
    } 
    
    // ignore to friend
    function isReqsFriend($uid, $friendID)
    {
        if ($uid  < 1 || $friendID < 1 || $uid==$friendID) return 0;

        $db = $this->getSlaveDB();
        try 
        {
            $t = time();
            $sql = "SELECT count(*) FROM friend_temp WHERE (uid=$uid AND reqsID=$friendID)";

            return $db->fetchOne($sql);  
        }
        catch (Exception $e)
        {
            return 0;
        }        
        
        return 0;    
    }       
    
    function searchFriend($uid, $kw="", $page=0, $limit=20, $recount = true)
    {
        $db=$this->getSlaveDB(); 
        $start=$page * $limit;
                
        $sql = "SELECT u.* 
                    FROM friend f 
                        INNER JOIN users u ON f.friendID = u.uid 
                    WHERE (f.uid=? AND u.name LIKE '%?%') 
                    ORDER BY f.timestamp DESC 
                    LIMIT $start, $limit";

        $sql_count = "SELECT count(*) 
                    FROM friend f 
                        INNER JOIN users u ON f.friendID = u.uid 
                    WHERE (f.uid=? AND u.name LIKE '%?%')";
        $count=null;
        if ($recount)
        {
            $count=$this->execScalar($sql_count, array($uid, $kw));
        }  
        $data=$this->execSelectQuery($sql, array($uid, $kw));
        return array("data" => $data, "count" => $count);        
    }
    
    public function listFriend($uid)
    {
        $uid=(int)$uid;
        if ($uid < 1) return null;
        
        $db=$this->getSlaveDB(); 
                
        $sql = "SELECT u.name, u.avatar, f.friendID
                    FROM friend f 
                        INNER JOIN users u ON f.friendID = u.uid 
                    WHERE f.uid=$uid ORDER BY f.point DESC ";

        $data=$this->execSelectQuery($sql);
        return $data;
    }
    
    public function reqsFriendListID($uid)
    {
        $uid=(int)$uid;
        if ($uid < 1) return null;
        
        $db=$this->getSlaveDB(); 
                
        $sql = "SELECT uid
                    FROM friend_temp ft 
                    WHERE ft.reqsID=$uid ";

        $data=$this->execSelectQuery($sql);
        return $data;        
    }    
    
    public function isreqsFriendListID($uid)
    {
        $uid=(int)$uid;
        if ($uid < 1) return null;
        
        $db=$this->getSlaveDB(); 
                
        $sql = "SELECT reqsID
                    FROM friend_temp ft 
                    WHERE ft.uid=$uid ";

        $data=$this->execSelectQuery($sql);
        return $data;        
    }      
    
    public function getFriendCount($uid)
    {
        $uid=(int)$uid;
        if ($uid < 1) return null;
        
        try 
        {        
            $db=$this->getSlaveDB();         
            $sql = "SELECT count(*) FROM friend WHERE uid = ?";
            return $db->fetchOne($sql, $uid);
        }
        catch (Exception $e)
        {
            return 0;
        }
        
    }
    
    public function getFriendReqsCount($uid)
    {
        $uid=(int)$uid;
        if ($uid < 1) return null;
        
        try 
        {    
            $db=$this->getSlaveDB();         
            $sql = "SELECT count(*) FROM friend_temp WHERE reqsID = $uid";
            
            return $db->fetchOne($sql);
        }
        catch (Exception $e)
        {
            return 0;
        }            
    }  
    
    public function cancelRequest($rid){
        $sql = "DELETE FROM friend_temp WHERE id=?";
        $db = $this->getMasterDB();
        return $db->query($sql,$rid);
    }
    
    public function getFriendInfo($uid, $fid)
    {
        $uid=(int)$uid;
        $fid=(int)$fid;
        if ($uid < 1 || $fid < 1) return null;
        
        try 
        {    
            $db=$this->getSlaveDB();         
            $sql = "SELECT u.uid, u.name, u.avatar, fr.testimonial FROM users u INNER JOIN friend fr ON u.uid=fr.uid WHERE fr.uid=? AND fr.friendID=? ";
            
            $data = $db->fetchRow($sql, array($uid, $fid)); 
              
            return $data;
        }
        catch (Exception $e)
        {
            return null;
        }            
    }         
    
}


